/*
 * 
 * 
 * 
 */
package com.xxs.template;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.xxs.model.Resource;
import com.xxs.utils.Filter;
import com.xxs.utils.FreemarkerUtils;
import com.xxs.utils.Order;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 资源列表
 */
public class ResourceListDirective extends BaseDirective {

	/** 变量名称 */
	private static final String VARIABLE_NAME = "resources";
	
	private static final String PARENT_ID = "parentId";

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		List<Resource> resources;
		boolean useCache = useCache(env, params);
		String cacheRegion = getCacheRegion(env, params);
		Integer parentId = FreemarkerUtils.getParameter(PARENT_ID, Integer.class, params);
		Integer count = getCount(params);
		List<Filter> filters = getFilters(params, Resource.class);
		List<Order> orders = getOrders(params);
		if (useCache) {
			resources = Resource.dao.findByParentId(parentId);
		} else {
			resources = Resource.dao.findByParentId(parentId);
		}
		setLocalVariable(VARIABLE_NAME, resources, env, body);
	}

}